package com.link.base.task;

import com.link.base.audittrail.model.AuditTrail;
import com.link.base.audittrail.model.AuditTrailDetail;
import com.link.core.util.JsonUtil;
import com.link.core.util.RedisUtil;
import com.link.core.util.StringUtils;
import com.link.core.util.redisclient.LinkRedisClient;

import java.util.ArrayList;
import java.util.List;

/**
 * 审计追踪
 *
 * @author zhenghongda
 * @date 2018/12/12 20:50
 */
public class AuditTrailUtil {

    public static final String LNK_AUDITTRAIL_KEY = "lnk_audittrail";

    public static final String LNK_AUDITTRAIL_DETAIL_KEY = "lnk_audittrail_detail";

    /**
     * 添加审计追踪头信息到redis
     */
    public static void setAuditTrail(AuditTrail auditTrail) {
        setMessage(LNK_AUDITTRAIL_KEY, JsonUtil.toString(auditTrail));
    }

    /**
     * 添加审计追踪头信息列表到redis
     */
    public static void setAuditTrailList(List<AuditTrail> auditTrailList) {
        List<String> list = new ArrayList<>();
        for (AuditTrail auditTrail : auditTrailList) {
            list.add(JsonUtil.toString(auditTrail));
        }
        setMessageList(LNK_AUDITTRAIL_KEY, list);
    }

    /**
     * 从redis获取审计追踪头信息
     */
    public static AuditTrail getAuditTrail() {
        String jsonString = getMessage(LNK_AUDITTRAIL_KEY);
        AuditTrail auditTrail = JsonUtil.fromJsonStringToT(jsonString, AuditTrail.class);
        return auditTrail;

    }

    /**
     * 从redis获取审计追踪头信息列表
     */
    public static List<AuditTrail> getAuditTrailList(int querySize) {
        List<AuditTrail> list = new ArrayList<>();
        List<String> jsonStrings = getMessageList(LNK_AUDITTRAIL_KEY, querySize);
        for (String jsonString : jsonStrings) {
            AuditTrail auditTrail = JsonUtil.fromJsonStringToT(jsonString, AuditTrail.class);
            list.add(auditTrail);
        }
        return list;
    }

    /**
     * 添加审计追踪行信息到redis
     */
    public static void setAuditTrailDetail(String auditTrailDetail) {
        setMessage(LNK_AUDITTRAIL_DETAIL_KEY, auditTrailDetail);
    }

    /**
     * 添加审计追踪行信息列表到redis
     */
    public static void setAuditTrailDetailList(List<AuditTrailDetail> auditTrailDetailList) {
        List<String> list = new ArrayList<>();
        for (AuditTrailDetail auditTraildetail : auditTrailDetailList) {
            list.add(JsonUtil.toString(auditTraildetail));
        }
        setMessageList(LNK_AUDITTRAIL_DETAIL_KEY, list);
    }

    /**
     * 从redis获取审计追踪行信息
     */
    public static String getAuditTrailDetail() {
        return getMessage(LNK_AUDITTRAIL_DETAIL_KEY);
    }

    /**
     * 从redis获取审计追踪行信息列表
     */
    public static List<AuditTrailDetail> getAuditTrailDetailList(int querySize) {
        List<AuditTrailDetail> list = new ArrayList<>();
        List<String> jsonStrings = getMessageList(LNK_AUDITTRAIL_DETAIL_KEY, querySize);
        for (String jsonString : jsonStrings) {
            AuditTrailDetail auditTrailDetail = JsonUtil.fromJsonStringToT(jsonString, AuditTrailDetail.class);
            list.add(auditTrailDetail);
        }
        return list;
    }

    /**
     * 添加信息到redis
     */
    private static void setMessage(String redisKey, String auditTrail) {
        LinkRedisClient jedis = null;
        try {
            jedis = RedisUtil.getJedis();
            jedis.lpush(redisKey, auditTrail);
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            RedisUtil.returnResource(jedis);
        }

    }

    /**
     * 批量添加信息到redis
     */
    private static void setMessageList(String redisKey, List<String> list) {
        LinkRedisClient jedis = null;
        try {
            jedis = RedisUtil.getJedis();
            for (String message : list) {
                jedis.lpush(redisKey, message);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }

    /**
     * 从redis获取信息
     */
    private static String getMessage(String redisKey) {
        String value = "";
        LinkRedisClient jedis = null;
        try {
            jedis = RedisUtil.getJedis();
            value = jedis.rpop(redisKey);
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            RedisUtil.returnResource(jedis);
        }
        return value;
    }

    /**
     * 从redis批量获取信息
     */
    private static List<String> getMessageList(String redisKey, int querySize) {
        List<String> list = new ArrayList<>();
        LinkRedisClient jedis = null;
        try {
            jedis = RedisUtil.getJedis();
            for (int i = 0; i < querySize; i++) {
                String value = jedis.rpop(redisKey);
                if (StringUtils.isNull(value)) {
                    break;
                }
                list.add(value);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        } finally {
            RedisUtil.returnResource(jedis);
        }
        return list;
    }


}
